package cn.yunzhisheng.vui.assistant.tv.view;

import cn.yunzhisheng.vui.assistant.preference.SessionPreferenceOfIntent;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.view.Display;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class FloatView extends LinearLayout {

    protected WindowManager.LayoutParams mWindowParams = new WindowManager.LayoutParams();
    protected WindowManager mWindowManager;

    protected Dialog mDialog;

    private boolean mIsShowing = false;
    protected Point mWindowSize = new Point();

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public FloatView(Context context) {
        super(context);
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = mWindowManager.getDefaultDisplay();
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
            mWindowSize.y = display.getHeight();
            mWindowSize.x = display.getWidth();
        } else {
            display.getSize(mWindowSize);
        }
    }

    public boolean isShowing() {
        return mIsShowing;
    }

    public void show() {
        if (mIsShowing) {
            return;
        }
        if (mDialog == null) {
            mDialog = new Dialog(getContext(), android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
            mDialog.setContentView(this);
            mDialog.getWindow().setAttributes(mWindowParams);
            mDialog.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {
                    getContext().sendBroadcast(new Intent(SessionPreferenceOfIntent.ACT_RECOGNIZE_OPERATE_CANCEL_TALK));
                }
            });
        }
        mIsShowing = true;
        mDialog.show();
        // mWindowManager.addView(this, mWindowParams);
    }

    public void hide() {
        if (mIsShowing) {
            mIsShowing = false;
            mDialog.dismiss();
            // mWindowManager.removeView(this);
        }
    }
}
